package com.zhuss.food.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.food.food.SearchFoodBo;
import com.zhuss.bean.vo.food.food.ListFoodVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.common.SelectVo;
import com.zhuss.enums.StartStopEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.food.db.dao.ext.FoodDao;
import com.zhuss.food.db.entity.Food;


/**
 * 查询餐品 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("foodRead")
public class FoodRead {
	
	@Autowired
	private FoodDao foodDao;
	
	
	public Map<String, Food> listMap(Integer area) {
		List<Food> list = foodDao.listAll(area, null);
		Map<String, Food> map = new HashMap<>();
		for (Food f : list) {
			map.put(f.getName(), f);
		}
		return map;
	}
	
	
	/**
	 * 根据 category 查询 可用的餐品列表下拉
	 * @param category
	 * @param status
	 * @param area
	 * @return
	 */
	public List<SelectVo> listByCategorySelect(Integer category, Integer area) {
		List<Food> list = listByCategory(category, area);
		List<SelectVo> result = new ArrayList<SelectVo>();
		for (Food item : list) {
			SelectVo sv = new SelectVo(item.getFoodId().intValue(), item.getName(), item.getFoodNo());
			result.add(sv);
		}
		return result;
	}
	
	
	/**
	 * 根据 category 查询 可用的餐品列表
	 * @param category
	 * @param status
	 * @param area
	 * @return
	 */
	public List<Food> listByCategory(Integer category, Integer area) {
		List<Food> result = foodDao.listByCategory(category, StartStopEnum.启用.key(), area);
		return result;
	}
	
	
	
	/**
	 * 根据名字查询餐品
	 * @param area 
	 * @param 
	 * @return
	 */
	public List<Food> list(String name, Integer area) {
		List<Food> result = foodDao.listByName(name, area);
		return result;
	}
	
	public List<Food> listNotId(String name, Long foodId, Integer area) {
		List<Food> result = new ArrayList<Food>();
		List<Food> list = list(name, area);
		for (Food c : list) {
			if (!c.getFoodId().equals(foodId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	
	/**
	 * 餐品ID列表
	 * @param area
	 * @return
	 */
	public Object listFoodId(Integer area) {
		List<Food> foodList = foodDao.listAll(area, null);
		List<Long> result = new ArrayList<>();
		for (Food food : foodList) {
			result.add(food.getFoodId());
		}
		return result;
	}
	
	/**
	 * 餐品
	 * @param area
	 * @return
	 */
	public Object listFood(Integer area) {
		List<Food> foodList = foodDao.listAll(area, null);
		List<ListVo> result = new ArrayList<>();
		transferList(foodList, result);
		return result;
	}
	
	/**
	 * 餐品 
	 * @param area
	 * @param status
	 * @return
	 */
	public Object listFoodId(Integer area, Integer status) {
		List<Food> foodList = foodDao.listAll(area, status);
		List<Long> result = new ArrayList<>();
		transferListId(foodList, result);
		return result;
	}
	
	private void transferListId(List<Food> foodList, List<Long> result) {
		for (Food item : foodList) {
			result.add(item.getFoodId());
		}
	}

	private void transferList(List<Food> foodList, List<ListVo> result) {
		for (Food item : foodList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getFoodId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 餐品列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchFoodBo bo) {
		PageVo<ListFoodVo> result = new PageVo<ListFoodVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(foodDao.list(bo));
        result.setTotal(foodDao.countList(bo));
		return result;
	}
	
	/**
	 * 餐品详情
	 * @param foodId
	 * @return
	 * @throws WpsException
	 */
	public Object detail(Long foodId) throws WpsException {
		Object result = foodDao.detail(foodId);
		if (result == null) {
			throw new WpsException(ExceptionCode.数据不存在);
		}
		return result;
	}
	
	

}
